/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmc.rra.controller;

import com.cmc.rra.forms.TransactionDetailsForm;
import com.cmc.rra.service.TransactionDetailsService;
import com.cmc.rra.service.impl.TransactionDetailsServiceImpl;
import com.cmc.rra.sub.TransactionDetails;
import com.cmc.rra.sub.impl.TransactionDetailsImpl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author cmc
 */
@Controller
@RequestMapping("/txForm")
public class TransactionDetailsController {
    
    TransactionDetails transactionDetails = null;
    TransactionDetailsService transactionDetailsService = null;
    
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String saveTransaction(@ModelAttribute TransactionDetailsForm transactionDetailsForm){
        
        transactionDetailsService = new TransactionDetailsServiceImpl();
        
        transactionDetails = new TransactionDetailsImpl();
        
        String name = transactionDetailsForm.getName();
        String location = transactionDetailsForm.getLocationCode();
        //Date dateOfPayment = transactionDetailsForm.getDateOfPayment();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        try {
            Date dateOfPayment = simpleDateFormat.parse(transactionDetailsForm.getDateOfPayment());
            transactionDetails.setDateOfPayment(dateOfPayment);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String taxTypeCode = transactionDetailsForm.getTaxTypeCode();
        String year = transactionDetailsForm.getYear();
        String taxTypeName = transactionDetailsForm.getTaxTypeName();
        Double amount = transactionDetailsForm.getAmount();
        Long transactionNumber = transactionDetailsForm.getTransactionNumber();       
        transactionDetails.setTransactionNumber(transactionNumber);
        transactionDetails.setName(name);
        transactionDetails.setLocationCode(location);
        
        transactionDetails.setTaxTypeCode(taxTypeCode);
        transactionDetails.setYear(year);
        transactionDetails.setTaxTypeName(taxTypeName);
        transactionDetails.setAmount(amount);
        
        transactionDetailsService.create(transactionDetails);
        
        return "success";
    }
}
